package FIFO;

public class FIFO {

	private int[] file;
	private int in;
	private int out;
	private int nbelt;
	
	
	public FIFO( int taille){
		in=0;
		out=0;
		nbelt=0;
		file = new int[taille];
		
	}
	
	public int getNbelt(){
		
		return nbelt;
	}
	
	public boolean equals(FIFO f){
		if (!(this.file.length == f.file.length)) return false;
		if (!(this.nbelt == f.nbelt )) return false;
		int i=0;
		int incrThis = this.out;
		int incrF = f.out;
		for ( ; i< this.file.length-1;i++){
			if (incrThis == this.file.length ) incrThis = 0;
			if (incrF == f.file.length-1)incrF = 0;
			
			if (!(this.file[incrThis]== f.file[incrF]))return false;
			incrThis++;
			incrF++;
		}
		return true;
	}
	
	public int[] parcours(){
		int retour[] = new int[this.nbelt];
		if (this.nbelt== 0) return retour;
		int i=0;
		int incrThis = this.out;
		System.out.println(this.out);
		for ( ; i < this.nbelt;i++){
			if (incrThis == this.nbelt) incrThis = 0;
			retour[i]= this.file[incrThis];

			incrThis++;
		}
		
		return retour;
						
	}
	
	public int compare(FIFO f){
		if (this.nbelt>f.nbelt)return (-1);
		if (this.nbelt==f.nbelt)return 0;
		return 1;
	}
	
	public String toString(){

		
		return "taille "+this.file.length+", nombre d elements "+nbelt ;
	}
	
	private boolean estVide(){
		
		return nbelt == 0;
	}
	
	private boolean estPleine(){

		return nbelt == this.file.length;
	}
	
	public void entrer(int e){

		 if (!this.estPleine()){
			 this.file[in] = e;
			 this.nbelt++;
			 this.in ++;
			 if (this.in == this.file.length) in = 0;		 
		 }else System.out.println ("La file est pleine!");
		
	}
	
	public int sortir(){
		int temp; 
		if (!this.estVide()){
			temp = this.file[out];
			this.nbelt--;
			this.out--;
			if (this.nbelt == this.file.length-1 || this.out <= -1) this.out = 0;
			return temp;
		 }else System.out.println ("La file est vide!");
		return(-1);
	}
	

}
